package com.xca.photogallery.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;
import com.xca.photogallery.R;
import com.xca.photogallery.entity.DayAvatar;
import com.xca.photogallery.entity.DayImage;
import com.xca.photogallery.utils.ImageDownloader;

import java.util.List;
import java.util.Random;

public class AvatarSecondAdapter extends RecyclerView.Adapter<AvatarSecondAdapter.ViewHolder> {
    private List<DayAvatar.AvatarFirstData.AvatarSecondData> mList ;
    private Context context ;
    private LayoutInflater inflater; // 用于加载布局
    private Activity activity ;
    public AvatarSecondAdapter(List<DayAvatar.AvatarFirstData.AvatarSecondData> list , Context context , Activity activity){
        this.mList = list ;
        this.context = context ;
        this.activity = activity ;
        this.inflater = LayoutInflater.from(context);
    }
    public void updateData(List<DayAvatar.AvatarFirstData.AvatarSecondData> list){
        mList.clear();
        mList = list ;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.avatar_item , parent , false);
        return new AvatarSecondAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DayAvatar.AvatarFirstData.AvatarSecondData data = mList.get(position);
        Glide.with(context)
                .load(data.getIcon())
                .into(holder.imageView);
        holder.textView.setText(data.getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDetailDialog(data);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    private void showDetailDialog(DayAvatar.AvatarFirstData.AvatarSecondData data){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View dialogView = inflater.inflate(R.layout.avatar_image_show , null);
        TextView tv1 = dialogView.findViewById(R.id.aTv2);
        TextView tv2 = dialogView.findViewById(R.id.aTv3);
        ImageView img = dialogView.findViewById(R.id.aImg);
        tv1.setText(data.getName());
        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageDownloader imageDownloader = new ImageDownloader(context , activity);
                imageDownloader.saveImage(data.getIcon());
            }
        });
        Glide.with(context)
                        .load(data.getIcon())
                                .into(img);
        builder.setView(dialogView)
                .create()
                .show();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView ;
        TextView textView ;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.a_item_img);
            textView = itemView.findViewById(R.id.a_item_text);
        }
    }
}
